import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _class, _class2;

/**
 * 接口字段权限维护
 * /ciam/role/field/:roleId/:permissionId
 * Field
 * @date 2019-07-10
 */
import React, { Component } from 'react';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import queryString from 'query-string';
import { Content, Header } from 'components/Page';
import { Button as ButtonPermission } from 'components/Permission';
import notification from 'utils/notification';
import intl from 'utils/intl';
import { getCurrentOrganizationId } from 'utils/utils';
import formatterCollections from 'cpaas-front/lib/utils/intl/formatterCollections';
import SearchForm from "./SearchForm";
import DataTable from "./DataTable";
import EditFormModal from "./EditFormModal";

function getFieldsValueByWrappedComponentRef(ref) {
  if (ref.current) {
    var form = ref.current.props.form;
    return form.getFieldsValue();
  }

  return {};
}

var Field = (_dec = connect(mapStateToProps, mapDispatchToProps), _dec2 = formatterCollections({
  code: ['ciam.roleManagement']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_Component) {
  _inherits(Field, _Component);

  var _super = _createSuper(Field);

  function Field(props) {
    var _this;

    _classCallCheck(this, Field);

    _this = _super.call(this, props);
    _this.state = {
      cachePagination: {},
      // 编辑后 需要之前的分页信息
      editFormModalVisible: false,
      // 编辑模态框显示
      isCreate: true,
      // 编辑表单是否是新建
      editRecord: {} // 编辑的记录

    };
    _this.searchFormRef = React.createRef();
    return _this;
  }

  _createClass(Field, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var fieldPermissionInit = this.props.fieldPermissionInit;
      fieldPermissionInit();
      this.reload();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var updateState = this.props.updateState;
      updateState({
        fieldPermissionDataSource: [],
        fieldPermissionPagination: {}
      });
    }
  }, {
    key: "reload",
    value: function reload() {
      var cachePagination = this.state.cachePagination;
      this.handleSearch(cachePagination);
    }
  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          queryFieldPermissions = _this$props.queryFieldPermissions,
          _this$props$match$par = _this$props.match.params,
          roleId = _this$props$match$par.roleId,
          permissionId = _this$props$match$par.permissionId;
      this.setState({
        cachePagination: pagination
      });
      var params = getFieldsValueByWrappedComponentRef(this.searchFormRef);
      queryFieldPermissions({
        roleId: roleId,
        permissionId: permissionId,
        params: _objectSpread(_objectSpread({}, params), pagination)
      });
    } // Header Button

  }, {
    key: "handleCreateBtnClick",
    value: function handleCreateBtnClick() {
      this.setState({
        editFormModalVisible: true,
        isCreate: true
      });
    } // SearchForm

  }, {
    key: "handleSearchFormSearch",
    value: function handleSearchFormSearch() {
      this.handleSearch();
    } // DataTable

  }, {
    key: "handleDataTableChange",
    value: function handleDataTableChange(page, filter, sort) {
      this.handleSearch({
        page: page,
        sort: sort
      });
    }
  }, {
    key: "handleRecordRemove",
    value: function handleRecordRemove(record) {
      var _this2 = this;

      var _this$props2 = this.props,
          removeFieldPermission = _this$props2.removeFieldPermission,
          permissionId = _this$props2.match.params.permissionId;
      removeFieldPermission({
        permissionId: permissionId,
        record: record
      }).then(function (res) {
        if (res) {
          notification.success();

          _this2.reload();
        }
      });
    }
  }, {
    key: "handleRecordEdit",
    value: function handleRecordEdit(record) {
      this.setState({
        editFormModalVisible: true,
        isCreate: false,
        editRecord: record
      });
    } // EditFormModal

  }, {
    key: "closeEditFormModal",
    value: function closeEditFormModal(callback) {
      this.setState({
        editFormModalVisible: false,
        isCreate: true,
        editRecord: {}
      }, callback);
    }
  }, {
    key: "handleEditFormModalCancel",
    value: function handleEditFormModalCancel() {
      this.closeEditFormModal();
    }
  }, {
    key: "handleEditFormModalOk",
    value: function handleEditFormModalOk(data) {
      var _this3 = this;

      var isCreate = this.state.isCreate;

      if (isCreate) {
        var _this$props3 = this.props,
            createFieldPermission = _this$props3.createFieldPermission,
            _this$props3$match$pa = _this$props3.match.params,
            roleId = _this$props3$match$pa.roleId,
            permissionId = _this$props3$match$pa.permissionId;
        createFieldPermission({
          roleId: roleId,
          permissionId: permissionId,
          record: data
        }).then(function (res) {
          if (res) {
            notification.success();

            _this3.closeEditFormModal(function () {
              _this3.reload();
            });
          }
        });
      } else {
        var _this$props4 = this.props,
            updateFieldPermission = _this$props4.updateFieldPermission,
            _this$props4$match$pa = _this$props4.match.params,
            _roleId = _this$props4$match$pa.roleId,
            _permissionId = _this$props4$match$pa.permissionId;
        var _this$state$editRecor = this.state.editRecord,
            editRecord = _this$state$editRecor === void 0 ? {} : _this$state$editRecor;
        updateFieldPermission({
          roleId: _roleId,
          permissionId: _permissionId,
          record: _objectSpread(_objectSpread({}, editRecord), data)
        }).then(function (res) {
          if (res) {
            notification.success();

            _this3.closeEditFormModal(function () {
              _this3.reload();
            });
          }
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props5 = this.props,
          dataSource = _this$props5.dataSource,
          pagination = _this$props5.pagination,
          fieldType = _this$props5.fieldType,
          permissionRule = _this$props5.permissionRule,
          loading = _this$props5.loading,
          saveLoading = _this$props5.saveLoading,
          removeLoading = _this$props5.removeLoading,
          organizationId = _this$props5.organizationId,
          search = _this$props5.location.search,
          _this$props5$match = _this$props5.match,
          path = _this$props5$match.path,
          _this$props5$match$pa = _this$props5$match.params,
          roleId = _this$props5$match$pa.roleId,
          permissionId = _this$props5$match$pa.permissionId;

      var _queryString$parse = queryString.parse(search.substring(1)),
          fromSource = _queryString$parse.fromSource,
          accessToken = _queryString$parse.access_token;

      var backPath = path.indexOf('/private') === 0 ? fromSource ? "/private/ciam/role-tree/api/".concat(roleId, "?fromSource=").concat(fromSource, "?access_token=").concat(accessToken) : "/private/ciam/role/api/".concat(roleId, "?access_token=").concat(accessToken) : fromSource ? "/ciam/role-tree/api/".concat(roleId, "?fromSource=").concat(fromSource) : "/ciam/role/api/".concat(roleId);
      var _this$state = this.state,
          isCreate = _this$state.isCreate,
          editFormModalVisible = _this$state.editFormModalVisible,
          editRecord = _this$state.editRecord;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('ciam.roleManagement.view.title.fieldPermission').d('接口字段权限配置'),
        backPath: backPath
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.create"),
          type: 'button',
          meaning: '接口字段权限配置-新建'
        }],
        type: "primary",
        onClick: this.handleCreateBtnClick,
        icon: "plus"
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(SearchForm, {
        onSearch: this.handleSearchFormSearch,
        permissionRule: permissionRule,
        wrappedComponentRef: this.searchFormRef
      }), /*#__PURE__*/React.createElement(DataTable, {
        path: path,
        dataSource: dataSource,
        pagination: pagination,
        onChange: this.handleDataTableChange,
        onRecordDelete: this.handleRecordRemove,
        onRecordEdit: this.handleRecordEdit,
        loading: loading,
        removeLoading: removeLoading
      }), /*#__PURE__*/React.createElement(EditFormModal, {
        visible: editFormModalVisible,
        isCreate: isCreate,
        fieldType: fieldType,
        permissionRule: permissionRule,
        onCancel: this.handleEditFormModalCancel,
        onOk: this.handleEditFormModalOk,
        dimensionValue: roleId,
        permissionDimension: "ROLE",
        permissionId: permissionId,
        organizationId: organizationId,
        record: editRecord,
        loading: saveLoading
      })));
    }
  }]);

  return Field;
}(Component), (_applyDecoratedDescriptor(_class2.prototype, "handleCreateBtnClick", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCreateBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearchFormSearch", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchFormSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDataTableChange", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDataTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRecordRemove", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRecordRemove"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRecordEdit", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRecordEdit"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEditFormModalCancel", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEditFormModalCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEditFormModalOk", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEditFormModalOk"), _class2.prototype)), _class2)) || _class) || _class);
export { Field as default };

function mapStateToProps(_ref) {
  var roleManagement = _ref.roleManagement,
      loading = _ref.loading;
  var fieldPermissionDataSource = roleManagement.fieldPermissionDataSource,
      fieldPermissionPagination = roleManagement.fieldPermissionPagination,
      fieldType = roleManagement.fieldType,
      permissionRule = roleManagement.permissionRule;
  return {
    organizationId: getCurrentOrganizationId(),
    fieldType: fieldType,
    permissionRule: permissionRule,
    dataSource: fieldPermissionDataSource,
    pagination: fieldPermissionPagination,
    loading: loading.effects['roleManagement/fieldPermissionInit'] || loading.effects['roleManagement/queryFieldPermissions'],
    saveLoading: loading.effects['roleManagement/updateFieldPermission'] || loading.effects['roleManagement/createFieldPermission'],
    removeLoading: loading.effects['roleManagement/removeFieldPermission']
  };
}

function mapDispatchToProps(dispatch) {
  return {
    fieldPermissionInit: function fieldPermissionInit(payload) {
      return dispatch({
        type: 'roleManagement/fieldPermissionInit',
        payload: payload
      });
    },
    queryFieldPermissions: function queryFieldPermissions(payload) {
      return dispatch({
        type: 'roleManagement/queryFieldPermissions',
        payload: payload
      });
    },
    updateFieldPermission: function updateFieldPermission(payload) {
      return dispatch({
        type: 'roleManagement/updateFieldPermission',
        payload: payload
      });
    },
    createFieldPermission: function createFieldPermission(payload) {
      return dispatch({
        type: 'roleManagement/createFieldPermission',
        payload: payload
      });
    },
    removeFieldPermission: function removeFieldPermission(payload) {
      return dispatch({
        type: 'roleManagement/removeFieldPermission',
        payload: payload
      });
    },
    updateState: function updateState(payload) {
      return dispatch({
        type: 'roleManagement/updateState',
        payload: payload
      });
    }
  };
}